Ext.define('ECAT.controller.FavoriteController', {
    extend: 'Ext.app.Controller',
	requires: ['ECAT.view.Favorite'],
    config: {
    	refs:{
    		favorite : 'favorite',
    		topBar : 'topbar',
    		buttomBar : 'buttombar'
    		
    	},
    	control: {
            'list_img_fav img': {
                tap: 'tapImg'
            },
            '#btn_fav' : {
            	tap : 'tapFavMenu'
            },
            '#btn_edit' : {
            	tap : 'tapEditBtn'
            },
            '#btn_del' : {
            	tap : 'tapDelBtn'
            },
            '#btn_finish' : {
            	tap : 'tapFinishBtn'
            }
        }
    },
    tapFavMenu : function(btn, e, eOpts){
    	var favorite = this.getFavorite();
    	if(Ext.Viewport.getActiveItem() !== favorite){
    		if(!favorite){
    			favorite = Ext.create('ECAT.view.Favorite');
    			Ext.Viewport.add(favorite);
    		}
    		favorite.view();
    		Ext.Viewport.setActiveItem(favorite);
    		this.getTopBar().toFavorite();
    		this.getButtomBar().setBtn(btn.id);
    	}
    },
    tapEditBtn : function(btn, e, eOpts){
	    this.getFavorite().edit();
		this.getTopBar().toFavEdit();
	    Ext.getCmp('btn_menu_down').disable();
    	
    },
    tapDelBtn : function(btn, e, eOpts){
     	var topbar =  this.getTopBar(),
			delfavcomfirm = topbar.delfavcomfirm;
        if (!delfavcomfirm) {
        	delfavcomfirm = topbar.delfavcomfirm = Ext.Viewport.add(Ext.create('ECAT.view.DelFavComfirm'));
        }
        delfavcomfirm.show();
    },
    tapFinishBtn : function(btn, e, eOpts){
		this.getFavorite().view();
		this.getTopBar().toFavorite();
	    Ext.getCmp('btn_menu_down').enable();
    },
    tapImg : function(img, e, eOpts){
    	if(this.getFavorite().list_img_fav.isEdit){ //收藏夹编辑,点击删除图片
    		this.delFav(img);
    	}else{ //收藏夹,点击查看详情
    		this.showImg(img);
    	}
    	
    },
    showImg : function(img){
    	var list = this.getFavorite().list_img_fav,
    	 	topbar = this.getTopBar();
    	ECAT.lib.showImageDetail(img, list.getParent(), list, '收藏',  topbar.toFavorite);
    },
    delFav : function(img){
    	var record = img.config.record,
    		img_name = record.get('name'),
    		del_imgs = this.getFavorite().list_img_fav.del_imgs,
    		img_index = del_imgs.indexOf(img_name),
    		c_img = img.getParent();
    	if(img_index == -1){
    		del_imgs.push(img_name);
    		c_img.setMasked({
    			xtype : 'deletemask',
    			listeners : {
    				scope : this,
    				tap : function(mask){
    					var c_mask = mask.getParent(),
    						img = c_mask.down('.img');
    					this.delFav(img);
    				}
    			}
    		});
    	}else{
    		del_imgs.splice(img_index, 1);
    		c_img.setMasked(false);
    	}
    	this.getTopBar().setBtnDel(del_imgs.length);
    }
});
